package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.ProcessNodeConvert;
import cn.com.blueInfo.bpm.template.entity.ProcessNode;
import cn.com.blueInfo.bpm.template.repository.ProcessNodeRepository;
import cn.com.blueInfo.bpm.template.request.ProcessNodeDTO;
import cn.com.blueInfo.bpm.template.response.ProcessNodeVO;
import cn.com.blueInfo.bpm.template.service.ProcessNodeService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 模板节点定义表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class ProcessNodeServiceImpl implements ProcessNodeService {

    @Autowired
    private ProcessNodeRepository processNodeRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private ProcessNodeConvert processNodeConvert;

    /**
     * 新增模板节点定义
     * @param processNodeDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<Integer> addProcessNode(ProcessNodeDTO processNodeDTO) {
        ProcessNode processNode = processNodeConvert.dtoToEntity(processNodeDTO);
        processNode.setId(snowflakeIdGenerator.nextId());
        Integer addSize = processNodeRepository.createProcessNode(processNode);
        return ResultInfo.<Integer>builder().data(addSize).message("模板节点定义成功新增 {} 条").build();
    }

    /**
     * 批量新增模板节点定义
     * @param processNodeDTOList 模板节点定义参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddProcessNode(List<ProcessNodeDTO> processNodeDTOList) {
        List<ProcessNode> processNodeList = processNodeConvert.dtoToEntity(processNodeDTOList);
        for (ProcessNode processNode : processNodeList) processNode.setId(snowflakeIdGenerator.nextId());
        Integer addSize = processNodeRepository.batchCreateProcessNode(processNodeList);
        return ResultInfo.<Integer>builder().data(addSize).message("模板节点定义成功新增 {} 条").build();
    }

    /**
     * 删除模板节点定义
     * @param processNodeId 模板节点定义ID
     */
    @Override
    public ResultInfo<Integer> removeProcessNode(Long processNodeId) {
        Integer removeSize = processNodeRepository.deleteProcessNode(processNodeId);
        return ResultInfo.<Integer>builder().data(removeSize).message("模板节点定义成功删除 {} 条").build();
    }

    /**
     * 批量删除模板节点定义
     * @param processNodeIdList 模板节点定义ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveProcessNode(List<Long> processNodeIdList) {
        Integer removeSize = processNodeRepository.batchDeleteProcessNode(processNodeIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("模板节点定义成功删除 {} 条").build();
    }

    /**
     * 更新部分模板节点定义
     * @param processNodeUpdatePartialDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<Integer> modifyProcessNodePartial(ProcessNodeDTO.UpdatePartialDTO processNodeUpdatePartialDTO) {
        ProcessNode processNode = processNodeConvert.dtoToEntity(processNodeUpdatePartialDTO);
        Integer modifySize = processNodeRepository.updateProcessNodePartial(processNode);
        return ResultInfo.<Integer>builder().data(modifySize).message("模板节点定义成功更新 {} 条").build();
    }

    /**
     * 更新全部模板节点定义
     * @param processNodeDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<Integer> modifyProcessNode(ProcessNodeDTO processNodeDTO) {
        ProcessNode processNode = processNodeConvert.dtoToEntity(processNodeDTO);
        Integer modifySize = processNodeRepository.updateProcessNode(processNode);
        return ResultInfo.<Integer>builder().data(modifySize).message("模板节点定义成功更新 {} 条").build();
    }

    /**
     * 查询一条模板节点定义
     * @param processNodeId 模板节点定义ID
     */
    @Override
    public ResultInfo<ProcessNodeVO> queryProcessNodeById(Long processNodeId) {
        ProcessNode processNode = processNodeRepository.getProcessNodeById(processNodeId);
        ProcessNodeVO processNodeVO = processNodeConvert.entityToVO(processNode);
        return ResultInfo.<ProcessNodeVO>builder().data(processNodeVO).message("模板节点定义查询成功").build();
    }

    /**
     * 分页查询模板节点定义
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<Page<ProcessNodeVO>> queryProcessNodePage(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        Page<ProcessNode> processNodePage = processNodeRepository.getProcessNodePage(processNodeQueryDTO);
        Page<ProcessNodeVO> resultData = processNodeConvert.convertPage(processNodePage);
        return ResultInfo.<Page<ProcessNodeVO>>builder()
                .data(resultData)
                .message("模板节点定义分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 模板节点定义 数量
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<Long> queryProcessNodeCount(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        Long resultCount = processNodeRepository.getProcessNodeCount(processNodeQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("模板节点定义数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 模板节点定义
     * @param processNodeQueryDTO 模板节点定义参数
     */
    @Override
    public ResultInfo<List<ProcessNodeVO>> queryProcessNodeList(ProcessNodeDTO.QueryDTO processNodeQueryDTO) {
        List<ProcessNode> processNodeList = processNodeRepository.getProcessNodeList(processNodeQueryDTO);
        List<ProcessNodeVO> resultData = processNodeConvert.entityListToVoList(processNodeList);
        return ResultInfo.<List<ProcessNodeVO>>builder()
                .data(resultData)
                .message("模板节点定义分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
